package com.xfusion.chatglm.data.domain.openai.service.rule.factory;

import com.alibaba.fastjson2.util.AnnotationUtils;
import com.xfusion.chatglm.data.domain.openai.annotation.LogicStrategy;
import com.xfusion.chatglm.data.domain.openai.service.rule.ILogicFilter;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 规则工厂
 */
@Service
public class DefaultLogicFactory {
    public Map<String, ILogicFilter>logicFilterMap = new ConcurrentHashMap<>();

    /**
     * 初始化过滤器
     * 遍历IlogicFilter列表，对每个过滤器对象使用注解获取逻辑模式，将逻辑模式与过滤器对象放入map中
     * @param logicFilters
     */
    public DefaultLogicFactory(List<ILogicFilter>logicFilters){
        logicFilters.forEach(logic->{
            LogicStrategy strategy = AnnotationUtils.findAnnotation(logic.getClass(), LogicStrategy.class);
            if (null!=strategy){
                logicFilterMap.put(strategy.logicMode().getCode(),logic);
            }
        });
    }

    /**
     * 返回过滤器的映射
     * @return
     */
    public Map<String,ILogicFilter>openLogicFilter(){
        return logicFilterMap;
    }

    /**
     * 规则逻辑枚举
     */
    public enum LogicModel{
        NULL("NULL", "放行不用过滤"),
        ACCESS_LIMIT("ACCESS_LIMIT", "访问次数过滤"),
        SENSITIVE_WORD("SENSITIVE_WORD","敏感词过滤"),
        USER_QUOTA("USER_QUOTA", "用户额度过滤"),
        ACCOUNT_STATUS("ACCOUNT_STATUS", "账户状态过滤"),
        ;
        private String code;
        private String info;
        LogicModel(String code,String info){
            this.code = code;
            this.info = info;
        }
        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }
    }
}
